<?php

namespace App\Admin\Controllers\System;

use App\Admin\Controllers\BaseAdminController;
use App\Models\System\HomeBanner;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;

class HomeBannerController extends BaseAdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(HomeBanner::with(['image']), function (Grid $grid) {
            $grid->enableDialogCreate();
            $grid->disableEditButton();
            $grid->showQuickEditButton('编辑');
            $grid->setDialogFormDimensions('80%', '90%');

            $grid->column('id')->sortable();
            $grid->column('title');
            $grid->column('image.url')->image('', '100', '60');
            $grid->column('jump_type')->using(trans('home-banner.options.jump_type'));
            $grid->column('link');
            $grid->column('appid');
            $grid->column('mini_path');
            $grid->column('status')->switch();
            $grid->column('creator.name');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new HomeBanner(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('description');
            $show->field('image_id');
            $show->field('jump_type');
            $show->field('link');
            $show->field('appid');
            $show->field('mini_path');
            $show->field('status');
            $show->field('creator_id');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new HomeBanner(), function (Form $form) {
            $form->text('title');
            $form->textarea('description');
            $form->uploadimagepro('image_id');
            $form->select('jump_type')->options(trans('home-banner.options.jump_type'))->when(1, function (Form $form) {
                $form->text('link');
            })->when(2, function (Form $form) {
                $form->text('appid');
                $form->text('mini_path');
            });
            $form->switch('status')->default(1);
            $form->hidden('creator_id')->default(Admin::user()->id);
        });
    }
}
